home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / glu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-18  |  7.4 KB  |  340 lines

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <ad709/tinygl/glu.h>
  4.  
  5.  
  6.  
  7.  
  8. void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
  9.                GLdouble centerx, GLdouble centery, GLdouble centerz,
  10.                GLdouble upx, GLdouble upy, GLdouble upz) {
  11.     GLfloat m[16];
  12.     GLfloat x[3], y[3], z[3];
  13.     GLfloat mag;
  14.     
  15.     /* Make rotation matrix */
  16.     
  17.     /* Z vector */
  18.     z[0] = eyex - centerx;
  19.     z[1] = eyey - centery;
  20.     z[2] = eyez - centerz;
  21.     mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
  22.     if (mag) {            /* mpichler, 19950515 */
  23.         z[0] /= mag;
  24.         z[1] /= mag;
  25.         z[2] /= mag;
  26.     }
  27.     
  28.     /* Y vector */
  29.     y[0] = upx;
  30.     y[1] = upy;
  31.     y[2] = upz;
  32.     
  33.     /* X vector = Y cross Z */
  34.     x[0] = y[1] * z[2] - y[2] * z[1];
  35.     x[1] = -y[0] * z[2] + y[2] * z[0];
  36.     x[2] = y[0] * z[1] - y[1] * z[0];
  37.     
  38.     /* Recompute Y = Z cross X */
  39.     y[0] = z[1] * x[2] - z[2] * x[1];
  40.     y[1] = -z[0] * x[2] + z[2] * x[0];
  41.     y[2] = z[0] * x[1] - z[1] * x[0];
  42.     
  43.     /* mpichler, 19950515 */
  44.     /* cross product gives area of parallelogram, which is < 1.0 for
  45.     * non-perpendicular unit-length vectors; so normalize x, y here
  46.     */    
  47.     mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  48.     if (mag) {
  49.         x[0] /= mag;
  50.         x[1] /= mag;
  51.         x[2] /= mag;
  52.     }
  53.     
  54.     mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  55.     if (mag) {
  56.         y[0] /= mag;
  57.         y[1] /= mag;
  58.         y[2] /= mag;
  59.     }
  60.     
  61. #define M(row,col)  m[col*4+row]
  62.     M(0, 0) = x[0];
  63.     M(0, 1) = x[1];
  64.     M(0, 2) = x[2];
  65.     M(0, 3) = 0.0;
  66.     M(1, 0) = y[0];
  67.     M(1, 1) = y[1];
  68.     M(1, 2) = y[2];
  69.     M(1, 3) = 0.0;
  70.     M(2, 0) = z[0];
  71.     M(2, 1) = z[1];
  72.     M(2, 2) = z[2];
  73.     M(2, 3) = 0.0;
  74.     M(3, 0) = 0.0;
  75.     M(3, 1) = 0.0;
  76.     M(3, 2) = 0.0;
  77.     M(3, 3) = 1.0;
  78. #undef M
  79.     glMultMatrixf(m);
  80.     
  81.     /* Translate Eye to Origin */
  82.     glTranslatef(-eyex, -eyey, -eyez);
  83. }
  84.  
  85.  
  86. void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat znear, GLfloat zfar) {
  87.     GLfloat xmin, xmax, ymin, ymax;
  88.     
  89.     ymax = znear * tan(fovy * 0.008726646);
  90.     ymin = -ymax;
  91.     xmin = ymin * aspect;
  92.     xmax = ymax * aspect;
  93.     
  94.     glFrustum(xmin, xmax, ymin, ymax, znear, zfar);
  95. }
  96.  
  97.  
  98.  
  99. /*
  100.  
  101.  
  102. static void normal3f( GLfloat x, GLfloat y, GLfloat z )    {
  103.     GLdouble mag;
  104.     
  105.     mag = sqrt( x*x + y*y + z*z );
  106.     if (mag>0.00001F) {
  107.         x /= mag;
  108.         y /= mag;
  109.         z /= mag;
  110.     }
  111.     glNormal3f( x, y, z );
  112. }
  113.  
  114.  
  115.  
  116. void drawTorus(float rc, int numc, float rt, int numt)
  117. {
  118.     int i, j, k;
  119.     double s, t;
  120.     double x, y, z;
  121.     double pi, twopi;
  122.     
  123.     pi = 3.14159265358979323846;
  124.     twopi = 2 * pi;
  125.     
  126.     for (i = 0; i < numc; i++) {
  127.         glBegin(GL_QUAD_STRIP);
  128.         for (j = 0; j <= numt; j++) {
  129.             for (k = 1; k >= 0; k--) {
  130.                 s = (i + k) % numc + 0.5;
  131.                 t = j % numt;
  132.                 
  133.                 x = cos(t*twopi/numt) * cos(s*twopi/numc);
  134.                 y = sin(t*twopi/numt) * cos(s*twopi/numc);
  135.                 z = sin(s*twopi/numc);
  136.                 glNormal3f(x, y, z);
  137.                 
  138.                 x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  139.                 y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  140.                 z = rc * sin(s*twopi/numc);
  141.                 glVertex3f(x, y, z);
  142.             }
  143.         }
  144.         glEnd();
  145.     }
  146. }
  147.  
  148.  
  149.  
  150. GLUquadric *gluNewQuadric(void) {
  151.     return NULL;
  152. }
  153.  
  154. void gluQuadricDrawStyle(GLUquadric *obj, int style)
  155. {
  156. }
  157.  
  158. void gluCylinder( GLUquadric *qobj,
  159.                  GLdouble baseRadius, GLdouble topRadius, GLdouble height,
  160.                  GLint slices, GLint stacks )
  161. {
  162.     GLdouble da, r, dr, dz;
  163.     GLfloat z, nz, nsign;
  164.     GLint i, j;
  165.     GLfloat du = 1.0 / slices;
  166.     GLfloat dv = 1.0 / stacks;
  167.     GLfloat tcx = 0.0, tcy = 0.0;
  168.     
  169.     nsign = 1.0;
  170.     
  171.     da = 2.0*M_PI / slices;
  172.     dr = (topRadius-baseRadius) / stacks;
  173.     dz = height / stacks;
  174.     nz = (baseRadius-topRadius) / height;  // Z component of normal vectors
  175.     
  176.     for (i=0;i<slices;i++) {
  177.         GLfloat x1 = -sin(i*da);
  178.         GLfloat y1 = cos(i*da);
  179.         GLfloat x2 = -sin((i+1)*da);
  180.         GLfloat y2 = cos((i+1)*da);
  181.         z = 0.0;
  182.         r = baseRadius;
  183.         tcy = 0.0;
  184.         glBegin( GL_QUAD_STRIP );
  185.         for (j=0;j<=stacks;j++) {
  186.             if (nsign==1.0) {
  187.                 normal3f( x1*nsign, y1*nsign, nz*nsign );
  188.                 glTexCoord2f(tcx, tcy);
  189.                 glVertex3f( x1*r, y1*r, z );
  190.                 normal3f( x2*nsign, y2*nsign, nz*nsign );
  191.                 glTexCoord2f(tcx+du, tcy);
  192.                 glVertex3f( x2*r, y2*r, z );
  193.             }
  194.             else {
  195.                 normal3f( x2*nsign, y2*nsign, nz*nsign );
  196.                 glTexCoord2f(tcx, tcy);
  197.                 glVertex3f( x2*r, y2*r, z );
  198.                 normal3f( x1*nsign, y1*nsign, nz*nsign );
  199.                 glTexCoord2f(tcx+du, tcy);
  200.                 glVertex3f( x1*r, y1*r, z );
  201.             }
  202.             z += dz;
  203.             r += dr;
  204.             tcy += dv;
  205.         }
  206.         glEnd();
  207.         tcx += du;
  208.     }
  209. }
  210.  
  211. // Disk (adapted from Mesa)
  212.  
  213. void gluDisk( GLUquadric *quad, GLdouble inner, GLdouble outer,
  214.              GLint slices, GLint loops) {
  215.     GLdouble a, da;
  216.     GLfloat dr;
  217.     GLfloat r1, r2, dtc;
  218.     GLint s, l;
  219.     GLfloat sa,ca;
  220.     
  221.     // Normal vectors
  222.     glNormal3f( 0.0, 0.0, +1.0 );
  223.     
  224.     da = 2.0*M_PI / slices;
  225.     dr = (outer-inner) / (GLfloat) loops;
  226.     
  227.     // texture of a gluDisk is a cut out of the texture unit square
  228.     // x, y in [-outerRadius, +outerRadius]; s, t in [0, 1] (linear mapping)
  229.     dtc = 2.0f * outer;
  230.     
  231.     r1 = inner;
  232.     for (l=0;l<loops;l++) {
  233.         r2 = r1 + dr;
  234.            glBegin( GL_QUAD_STRIP );
  235.            for (s=0;s<=slices;s++) {
  236.                if (s==slices) a = 0.0;
  237.                else  a = s * da;
  238.                sa = sin(a); ca = cos(a);
  239.                glTexCoord2f(0.5+sa*r2/dtc,0.5+ca*r2/dtc);
  240.                glVertex2f( r2*sa, r2*ca );
  241.                glTexCoord2f(0.5+sa*r1/dtc,0.5+ca*r1/dtc);
  242.                glVertex2f( r1*sa, r1*ca );
  243.            }
  244.            glEnd();
  245.            r1 = r2;
  246.     }
  247.     
  248. }
  249.  
  250.  
  251.  
  252. void gluSphere(GLUquadric *quad, float radius,int slices,int stacks) {
  253.     float rho, drho, theta, dtheta;
  254.     float x, y, z;
  255.     float s, t, ds, dt;
  256.     int i, j, imin, imax;
  257.     int normals;
  258.     float nsign;
  259.     
  260.     normals=1;
  261.     nsign=1;
  262.     
  263.     drho = M_PI / (float) stacks;
  264.     dtheta = 2.0 * M_PI / (float) slices;
  265.     
  266.     // draw +Z end as a triangle fan
  267.     glBegin( GL_TRIANGLE_FAN );
  268.     glNormal3f( 0.0, 0.0, 1.0 );
  269.     glTexCoord2f(0.5,0.0);
  270.     glVertex3f( 0.0, 0.0, nsign * radius );
  271.     for (j=0;j<=slices;j++) {
  272.         theta = (j==slices) ? 0.0 : j * dtheta;
  273.         x = -sin(theta) * sin(drho);
  274.         y = cos(theta) * sin(drho);
  275.         z = nsign * cos(drho);
  276.         if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign );
  277.         glVertex3f( x*radius, y*radius, z*radius );
  278.     }
  279.     glEnd();
  280.     
  281.     
  282.     ds = 1.0 / slices;
  283.     dt = 1.0 / stacks;
  284.     t = 1.0;  // because loop now runs from 0
  285.     if (1) {
  286.         imin = 0;
  287.         imax = stacks;
  288.     }
  289.     else {
  290.         imin = 1;
  291.         imax = stacks-1;
  292.     }
  293.     
  294.     // draw intermediate stacks as quad strips
  295.     for (i=imin;i<imax;i++) {
  296.         rho = i * drho;
  297.         glBegin( GL_QUAD_STRIP );
  298.         s = 0.0;
  299.         for (j=0;j<=slices;j++) {
  300.             theta = (j==slices) ? 0.0 : j * dtheta;
  301.             x = -sin(theta) * sin(rho);
  302.             y = cos(theta) * sin(rho);
  303.             z = nsign * cos(rho);
  304.             if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign );
  305.             glTexCoord2f(s,1-t);
  306.             glVertex3f( x*radius, y*radius, z*radius );
  307.             x = -sin(theta) * sin(rho+drho);
  308.             y = cos(theta) * sin(rho+drho);
  309.             z = nsign * cos(rho+drho);
  310.             if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign );
  311.             glTexCoord2f(s,1-(t-dt));
  312.             s += ds;
  313.             glVertex3f( x*radius, y*radius, z*radius );
  314.         }
  315.         glEnd();
  316.         t -= dt;
  317.     }
  318.     
  319.     // draw -Z end as a triangle fan
  320.     glBegin( GL_TRIANGLE_FAN );
  321.     glNormal3f( 0.0, 0.0, -1.0 );
  322.     glTexCoord2f(0.5,1.0);
  323.     glVertex3f( 0.0, 0.0, -radius*nsign );
  324.     rho = M_PI - drho;
  325.     s = 1.0;
  326.     t = dt;
  327.     for (j=slices;j>=0;j--) {
  328.         theta = (j==slices) ? 0.0 : j * dtheta;
  329.         x = -sin(theta) * sin(rho);
  330.         y = cos(theta) * sin(rho);
  331.         z = nsign * cos(rho);
  332.         if (normals)  glNormal3f( x*nsign, y*nsign, z*nsign );
  333.         glTexCoord2f(s,1-t);
  334.         s -= ds;
  335.         glVertex3f( x*radius, y*radius, z*radius );
  336.     }
  337.     glEnd();
  338. }
  339.  
  340. */